/**
 *  Copyright (c)  2011-2020 Panguso, Inc.
 *  All rights reserved.
 *
 *  This software is the confidential and proprietary information of Panguso, 
 *  Inc. ("Confidential Information"). You shall not
 *  disclose such Confidential Information and shall use it only in
 *  accordance with the terms of the license agreement you entered into with Panguso.
 */

package storage.dbhelper;

import cn.edu.thu.log.web.service.PropertiesReaderServices;
import cn.edu.thu.log.web.service.impl.PropertiesReaderServicesImpl;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.cassandra.thrift.ColumnOrSuperColumn;
import org.apache.cassandra.thrift.KeySlice;

import storage.dbobject.DbLogTokenizer;

import storage.util.StorageManager;
/**
 * 
 * @author fenyo rome
 *
 */
public class LogTokenizerHelper {

	/**
	 * 将logTokenizer保存到LaUDMS数据库中
	 * 
	 * @param logTokenizer
	 *            要存储的模型对象
	 */
	public void saveLogTokenizer(DbLogTokenizer logTokenizer) {
		PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
		StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"),
				x1.getProperties("db_columnFamily_logTokenizer"));
		// sm.open();
		sm.setRowKey(logTokenizer.getRowKey());
		sm.put(DbLogTokenizer.CL_NAME, logTokenizer.getName());
		sm.put(DbLogTokenizer.CL_VALUE, logTokenizer.getValue());
		// sm.put(logTokenizer.cl_chinese, logTokenizer.getChinese());
		// sm.close();

	}

	/**
	 * 将一系列logTokenizer保存到LaUDMS数据库中
	 * 
	 * @param logTokenizers
	 *            要存储的模型对象列表
	 */
	public void saveLogTokenizers(List<DbLogTokenizer> logTokenizers) {
		PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
		StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"),
				x1.getProperties("db_columnFamily_logTokenizer"));
		// sm.open();
		for (int i = 0; i < logTokenizers.size(); i++) {
			DbLogTokenizer logTokenizer = logTokenizers.get(i);
			sm.setRowKey(logTokenizer.getRowKey());
			sm.put(DbLogTokenizer.CL_NAME, logTokenizer.getName());
			sm.put(DbLogTokenizer.CL_VALUE, logTokenizer.getValue());
			// sm.put(logTokenizer.cl_chinese, logTokenizer.getChinese());
		}
		// sm.close();
	}
	/**
	 * 
	 * @param columnName
	 * columnName
	 * @param value
	 * value
	 * @return
	 */
	public List<DbLogTokenizer> getLogTokenizers(final String columnName,
			final String value) {
		List<DbLogTokenizer> result = new LinkedList<DbLogTokenizer>();
		PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
		StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"),
				x1.getProperties("db_columnFamily_logTokenizer"));
		// sm.open();
		List<KeySlice> crs = sm.get(columnName, value);
		try {
			for (KeySlice ks : crs) {
				String rowKey = new String(ks.getKey());
				String name = null, valueL = null, chinese = null;
				for (ColumnOrSuperColumn column : ks.getColumns()) {
					String columnsName = new String(column.getColumn()
							.getName());
					String columnsValue = new String(column.getColumn()
							.getValue());
					if (columnsName.equals(DbLogTokenizer.CL_NAME)) {
						name = columnsValue;
					} else if (columnsName.equals(DbLogTokenizer.CL_VALUE)) {
						valueL = columnsValue;
					}
				}
				DbLogTokenizer pLogTokenizer = new DbLogTokenizer(rowKey, name,
						valueL);
				result.add(pLogTokenizer);
			}
		} catch (Exception ex) {
			Logger.getLogger(LogTokenizerHelper.class.getName()).log(
					Level.SEVERE, null, ex);
		} 
		return result;
	}
	/**
	 * 
	 * @param rowKey
	 * rowKey
	 */
	public void deleteLogTokenizer(final String rowKey) {
		PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
		StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"),
				x1.getProperties("db_columnFamily_logTokenizer"));
		// sm.open();
		sm.delete(rowKey);
		// sm.close();
	}
	/**
	 * 
	 * @return ArrayList<String>
	 */
	public ArrayList<String> getAllLogTokenizerName() {
		PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
		StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"),
				x1.getProperties("db_columnFamily_logTokenizer"));
		// sm.open();
		List<KeySlice> crs = sm.getAll();
		ArrayList<String> logTokenizerNameList = new ArrayList<String>();
		try {
			for (KeySlice ks : crs) {
				String name = null;
				for (ColumnOrSuperColumn column : ks.getColumns()) {
					String columnsName = new String(column.getColumn()
							.getName());
					String columnsValue = new String(column.getColumn()
							.getValue());
					if (columnsName.equals(DbLogTokenizer.CL_NAME)) {
						name = columnsValue;
					}
				}
				if (!logTokenizerNameList.contains(name)) {
					logTokenizerNameList.add(name);
				}
			}
		} catch (Exception ex) {
			Logger.getLogger(LogTokenizerHelper.class.getName()).log(
					Level.SEVERE, null, ex);
		} 
		return logTokenizerNameList;
	}
}
